package business.service.impl;

import model.User;
import persistence.exceptions.RepositoryException;
import persistence.service.UserPersistenceService;
import business.exceptions.BusinessServiceException;
import business.service.UserBusinessService;

public class UserBusinessServiceImpl implements UserBusinessService {

	private final UserPersistenceService persistenceService;

	public UserBusinessServiceImpl(UserPersistenceService persistenceService) {
		super();
		this.persistenceService = persistenceService;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Integer save(User user) throws BusinessServiceException {
		Integer id = 0;
		try {
			id = persistenceService.save(user);
		} catch (RepositoryException ex) {
			System.out.println(ex);
			throw new BusinessServiceException("Error saving user!", ex);
		}
		return id;

	}

	@Override
	public String getPosition(int id) throws BusinessServiceException {
		String position = "";
		try {
			position = persistenceService.getPosition(id);
		} catch (RepositoryException ex) {
			System.out.println(ex);
			throw new BusinessServiceException("Error calculating index!", ex);
		}
		return position;
	}

	@Override
	public User getPerson(int id, String op) throws BusinessServiceException {
		User p = null;
		try{
			p = persistenceService.getPerson(id, op);
		} catch (RepositoryException ex) {
			System.out.println(ex);
			throw new BusinessServiceException(
					"Error reading from the database!", ex);
		}
		return p;
	}

	@Override
	public int getMinMaxId(String op) throws BusinessServiceException {
		int x = 0;
		try{
			x = persistenceService.getMinMaxId(op);
		}catch (RepositoryException ex){
			System.out.println(ex);
			throw new BusinessServiceException("Error calculating index!", ex);
		}
		return x;
	}

	@Override
	public int insertPerson(User p) throws BusinessServiceException {
		int id = 0;
		try {
			id = persistenceService.insertPerson(p);
		} catch (RepositoryException ex) {
			System.out.println(ex);
			throw new BusinessServiceException("Error saving the user!", ex);
		}
		return id;
	}

	@Override
	public void updatePerson(User p) throws BusinessServiceException {
		try {
			persistenceService.updatePerson(p);
		} catch (RepositoryException ex) {
			System.out.println(ex);
			throw new BusinessServiceException("Error saving the user info!",
					ex);
		}

	}

	@Override
	public void deletePerson(int id) throws BusinessServiceException {
		try {
		persistenceService.deletePerson(id);
		} catch (RepositoryException ex) {
			System.out.println(ex);
			throw new BusinessServiceException("Error deleting the uesr!", ex);
		}
	}

	@Override
	public User[] searchUser(String user) throws BusinessServiceException {
		User[] userList = null;
		try {
			userList = persistenceService.searchUser(user);
		} catch (RepositoryException ex) {
			System.out.println(ex);
			throw new BusinessServiceException("Error searching for users!");
		}
		return userList;
	}

}
